package com.wmh.baseservice.operationaid.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.common.utils.web.UserUtil;
import com.wmh.baseservice.operationaid.entity.KeyWordUsageRecord;
import com.wmh.baseservice.operationaid.pojo.vo.keyWordUsageRecord.KeyWordUsageRecordCommitReqVO;
import com.wmh.baseservice.operationaid.pojo.vo.keyWordUsageRecord.KeyWordUsageRecordMaskReqVO;
import com.wmh.baseservice.operationaid.service.KeyWordUsageRecordService;
import com.wmh.baseservice.operationaid.enums.KeyWordUsageEnums;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

import static com.wmh.baseservice.common.utils.returnbody.ReturnBody.success;

/**
 * @author dongaolong
 * @createDate 2023-06-20
 */
@Validated
@RestController
@RequestMapping("operationAid/KeyWordUsageRecord")
@Api(tags = "关键词作用记录")
@Slf4j
public class KeyWordUsageRecordController {

    @Resource
    private KeyWordUsageRecordService keyWordUsageRecordService;

    @Resource
    private UserUtil userUtil;

    @ApiOperation("删除")
    @PostMapping("/delete")
    public String delete(Long id) {
        if (!keyWordUsageRecordService.removeById(id)) {
            return ReturnBody.error("删除失败,请检查后再次操作");
        }
        return success();
    }

    @ApiOperation("获取关键词作用记录列表")
    @PostMapping("/list")
    public String list() {
        List<KeyWordUsageRecord> list = keyWordUsageRecordService.list(new QueryWrapper<KeyWordUsageRecord>()
                .eq(KeyWordUsageRecord.COL_ID, userUtil.getId())
                .orderByDesc(KeyWordUsageRecord.COL_CREATE_TIME));
        return ReturnBody.success(list);
    }

    @ApiOperation("添加")
    @PostMapping("/commit")
    public String commit(KeyWordUsageRecordCommitReqVO reqVO) {
        keyWordUsageRecordService.commitUsageRecord(reqVO);
        return ReturnBody.success();
    }

    @ApiOperation("添加标记")
    @PostMapping("/markV2")
    public String mark(@Valid KeyWordUsageRecordMaskReqVO reqVO) {
        keyWordUsageRecordService.markUsageRecord(reqVO);
        return ReturnBody.success();
    }

    @ApiOperation("查询")
    @PostMapping("/query")
    public String getQueryKeyWordUsageRecord(@NotBlank(message = "品类名称未填写") String categoryName,
                                             @NotNull(message = "用途类型不能为空") KeyWordUsageEnums type,
                                             Long groupId
    ) {
        return ReturnBody.success(keyWordUsageRecordService.getQueryKeyWordUsageRecord(userUtil.getId(), categoryName, type,groupId));
    }

    @ApiOperation("更新指定用途记录组名")
    @PostMapping("/updateGroup")
    public String updateGroupId(
            @NotBlank(message = "未提交记录主键列表")
            @Size(min=3,message = "未提交记录主键列表")
            String idsJson,
            @NotNull(message = "未提交分组主键")
            Long groupId
    ){
        keyWordUsageRecordService.updateGroupId(JSON.parseArray(idsJson,Long.class),groupId);
        return ReturnBody.success();
    }

    @ApiOperation("批量修改状态")
    @PostMapping("/status")
    public String batchUpdateStatusKeyWordUsageRecord(@Size(min = 3, message = "请上传主键")
                                                          @NotBlank(message = "请上传主键") String ids,
                                                      @NotNull(message = "用途类型不能为空") KeyWordUsageEnums type) {
        keyWordUsageRecordService.getStatusKeyWordUsageRecord(JSON.parseArray(ids, String.class), type);
        return ReturnBody.success();
    }
}